import api from '../api/index.js';

const state = {
    class: [],
    mainTypes: [],
    guessLike: [],
    maitKey: "",
    sortFilter: [],
    miniWallkey: "",
    miniWall: "",
    sortKey: "pop",
}

const actions = {
    ClassPageLoad(store) {
        api.multigetData({
                appPlat: "m",
                pids: "117330"
            }).then((data) => {
                if (!data.success) {
                    return Promise.reject("错误");
                }
                store.commit("setMaitKey", data.data[117330].list[0].maitKey);
                store.commit("setMiniWallkey", data.data[117330].list[0].miniWallkey);
                store.commit("setClass", data.data[117330].list);
                return {
                    pid: data.data[117330].list[0].maitKey,
                    fcid: data.data[117330].list[0].miniWallkey
                }
            }).then(({ pid, fcid }) => {
                store.dispatch("ClassCommodity", pid);
                store.dispatch("ClassMiniWall", fcid);
            })
            .catch((error) => {

            })
    },
    ClassCommodity(store, size) {
        api.classCommodity({
            appPlat: "m",
            pid: size,
        }).then((data) => {
            console.log(data);
            if (!data.success) {
                return Promise.reject("错误");
            }
            store.commit("setMainTypes", data.data.list);
        }).catch((error) => {

        })
    },
    ClassMiniWall(store, type, page = 1) {
        api.classMiniWall({
            fcid: type,
            page: page,
            sort: store.state.sortKey,
            cKey: "h5-cube"
        }).then((data) => {
            if (!data.success) {
                return Promise.reject("错误");
            }
            store.commit("setSortFilter", data.result.sortFilter);
            store.commit("setMiniWall", data.result.wall.docs);
        }).catch((error) => {

        })
    },

}

const mutations = {
    setClass(state, nv) {
        state.class = nv;
    },
    setMainTypes(state, nv) {
        state.mainTypes = nv;
    },
    setGuessLike(state, nv) {
        state.guessLike = nv;
    },
    setMaitKey(state, nv) {
        state.maitKey = nv;
    },
    setMiniWallkey(state, nv) {
        state.miniWallkey = nv;
    },
    setMiniWall(state, nv) {
        state.miniWall = nv;
    },
    setSortFilter(state, nv) {
        state.sortFilter = nv;
    },
    setSortKey(state, nv) {
        state.sortKey = nv;
    },

}

export default {
    state,
    actions,
    mutations
}